// 导入axios
import axios from 'axios'
// 导入vuex
import store from '@/store/index'

// 创建一新的axios实例
const request = axios.create({
  baseURL: 'http://interview-api-t.itheima.net',
  timeout: 5000
})

// 添加请求拦截器
request.interceptors.request.use(
  (config) => {
    // 添加请求头
    config.headers.Authorization = 'Bearer ' + store.state.user.token
    return config
  },
  (error) => Promise.reject(error)
)

// 添加响应拦截器
request.interceptors.response.use(
  (response) => response.data,
  (error) => Promise.reject(error)
)

// 导出配置好的request
export default request
